﻿// Apache-2.0 License
// Copyright (c) 2021-2022 SuperJeff
// 微信:15906588664  QQ:1024489

using Admin.Net.InformationPlatform.Entity;
using Admin.Net.InformationPlatform.Service.SRM.Dto;
using Admin.NET.Application.Const;
using Admin.NET.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Office.Interop.Word;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.XWPF.UserModel;
using SqlSugar;
using System.Security.Cryptography;
using System.Text;
using OfficeOpenXml;
using NewLife.Reflection;
using RazorEngine.Compilation.ImpromptuInterface.Optimization;
using System;
using System.ComponentModel;

namespace Admin.Net.InformationPlatform.Service.SRM
{
    /// <summary>
    /// SRM管理
    /// </summary>
    [ApiDescriptionSettings(groups: "SRM", Name = "供应商评级管理", Tag = "供应商评级管理", Order = 200)]

    public class SrmSupplierMarkService : IDynamicApiController, ITransient
    {
        /// <summary>
        /// 单例数据链接
        /// </summary>
        ISqlSugarClient db;
        /// <summary>
        /// T100数据库
        /// </summary>
        private SqlSugarProvider _T100Db;
        private SqlSugarProvider _SjDb;
        private SqlSugarProvider _Dilon;

        /// <summary>
        /// 用户信息
        /// </summary>
        TokenUser _User;


        /// <summary>
        /// srm采购管理模块
        /// </summary>
        /// <param name="db"></param>
        public SrmSupplierMarkService(ISqlSugarClient db)
        {
            this.db = db;
            this._T100Db = this.db.AsTenant().GetConnection(T100Const.ConfigId);
            this._SjDb = this.db.AsTenant().GetConnection(SJDB.ConfigId);
            this._Dilon = this.db.AsTenant().GetConnection(Dilon.ConfigId);

            // 获取 `Jwt` 存储的信息 企业编号与据点信息
            _User = Util.SjCommonUtil.GetT100UserTokenInfo(_T100Db,_Dilon);


        }
        #region 供应商数据导入
        /// <summary>
        /// 供应商数据导入
        /// </summary>
        /// <returns></returns>
        [HttpPost("/SRM/ImportCustomerRatings")]
        public async Task<ActionResult<dynamic>> ImportCustomerRatings(IFormFile file)
        {
            ////判断是否选择文件
            //if (file == null || file.Length <= 0)
            //{
            //    return new { success = false, msg = "请选择导入文件！", result = "" };
            //}
            ////判断文件后缀
            //string Express = Path.GetExtension(file.FileName);
            //if (Express != ".xlsx"&& Express != ".xls")
            //{
            //    throw Oops.Oh("请上传文件扩展名格式为'.xlsx'或'.xls'的文件!");
            //    //return BadRequest("请上传文件扩展名格式为'.xlsx'的文件!");
            //}
            //// 文件名md5
            //string filename = Md5FileHash(file.OpenReadStream())+DateTime.Now;
            ////文件名+尾缀
            //filename += Express;
            ////存储目录
            //var path = Path.Combine(Directory.GetCurrentDirectory(), "File", "Excel");
            ////查看是否存在，不存在创建
            //if (!Directory.Exists(path))
            //{
            //    Directory.CreateDirectory(path);
            //}
            //string filePath = Path.Combine(path, filename);
            //FileStream stream = new(filePath, FileMode.OpenOrCreate);
            //await file.CopyToAsync(stream);
            //stream.Close();



            //判断是否选择文件
            if (file == null || file.Length <= 0)
            {
                return new { success = false, msg = "请选择导入文件！", result = "" };
            }
            //拼接出文件保存到的路径
            var path = Path.Combine(Directory.GetCurrentDirectory(), "File", "Excel");
            //判断文件后缀
            string Express = Path.GetExtension(file.FileName);
            if (Express != ".xlsx" && Express != ".xls")
            {
                throw Oops.Oh("请上传文件扩展名格式为'.xlsx'或'.xls'的文件!");
                //return BadRequest("请上传文件扩展名格式为'.xlsx'的文件!");
            }
            // 文件名md5
            string filename = Path.GetFileNameWithoutExtension(file.FileName) + '-' + Md5FileHash(file.OpenReadStream());
            //文件名+尾缀
            filename += Express;
            //检查是否存在文件夹
            if (false == System.IO.Directory.Exists(path))
            {
                //创建文件夹
                System.IO.Directory.CreateDirectory(path);
            }
            string filePath = path + $@"\{filename}";
            //保存文件
            using (FileStream fs = System.IO.File.Create(filePath))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            //获取指定地址的文件
            using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                // 定义一个用于存储excel表数据的集合
                // 以下模拟存储的数据集合对应实体类为T，属性有Name、Age和CreateTime
                List<SRMGYSPJ> data = new List<SRMGYSPJ>();
                List<SRMGYSPJ> updata = new List<SRMGYSPJ>();
                // 创建工作簿
                IWorkbook workbook = null;
                try
                {
                    // .xlsx格式：XSSFWorkBook
                    if (filename.IndexOf(".xlsx", StringComparison.Ordinal) > 0)
                    {
                        workbook = new XSSFWorkbook(fs);
                    }
                    // .xls格式：HSSFWorkBook
                    else if (filename.IndexOf(".xls", StringComparison.Ordinal) > 0)
                    {
                        workbook = new HSSFWorkbook(fs);
                    }
                    // 创建一个 FormulaEvaluator 对象
                    var evaluator = workbook.GetCreationHelper().CreateFormulaEvaluator();

                    // workbook不为空(文件格式正确)才继续执行
                    if (workbook != null)
                    {
                        // 读取第一个sheet(表)
                        ISheet sheet = workbook.GetSheetAt(0);
                        int rowCount = sheet.LastRowNum;// 获取最后一行的索引值（总行数）
                                                        // 确定表中一定有数据再继续执行
                        if (rowCount > 0)
                        {
                          
                            // 循环填入数据
                            // FirstRowNum 第一个逻辑行
                            for (int a = sheet.FirstRowNum; a <= rowCount; a++)
                            {
                              
                                IRow row = sheet.GetRow(a);


                                if (row != null) { 
                             
                                //判断单元格是否有值并且为日期格式
                                if (!string.IsNullOrEmpty(row.GetCell(0)?.ToString()))
                                {
                                    if (row.GetCell(0).CellType == CellType.Numeric) { 
                                    var time = DateTime.Parse(row.GetCell(0).DateCellValue.ToString("yyyy-MM-dd"));


                                  // string cellValue = row.GetCell(18).CellFormula?.ToString();
                                  // string cellValue1 = GetCellTextValue(row,18);

                                   //string SRMLB = row.GetCell(1)?.ToString();
                                   // string SRMGYSBH = row.GetCell(2)?.ToString();
                                   // string SRMGYSMC = row.GetCell(3)?.ToString();
                                   // string SRMJYZPC = row.GetCell(4)?.ToString();
                                   // string SRMHGPC = row.GetCell(5)?.ToString();
                                   // string SRMHGL = row.GetCell(6)?.ToString();
                                   // string SRMHGLDF = row.GetCell(7)?.ToString();
                                   // string SRMZPC = row.GetCell(8)?.ToString();
                                   // string SRMJSRKPC = row.GetCell(9)?.ToString();
                                   // string SRMDCL = row.GetCell(10)?.ToString();
                                   // string SRMDCLDF = row.GetCell(11)?.ToString();
                                   // string SRMDDJQHF = row.GetCell(12)?.ToString();
                                   // string SRMJJD = row.GetCell(13)?.ToString();
                                   // string SRMYCCLPD = row.GetCell(14)?.ToString();
                                   // string SRMBZJFZQ = row.GetCell(15)?.ToString();
                                   // string SRMSJJFRQ = row.GetCell(16)?.ToString();
                                   // string SRMCGZQDF = row.GetCell(17)?.ToString();
                                   // string SRMZF = row.GetCell(18)?.ToString();
                                   // string SRMPJ = row.GetCell(19)?.ToString();
                                            
                                    string SRMLB = GetCellTextValue(row, 1);
                                    string SRMGYSBH = GetCellTextValue(row, 2);
                                            string SRMGYSMC = GetCellTextValue(row, 3);
                                            string SRMJYZPC = GetCellTextValue(row, 4);
                                            string SRMHGPC = GetCellTextValue(row, 5);
                                            string SRMHGL = GetFormattedCellValue(row, 6);
                                            string SRMHGLDF = GetCellTextValue(row, 7);
                                            string SRMZPC = GetCellTextValue(row, 8);
                                            string SRMJSRKPC = GetCellTextValue(row, 9);
                                            string SRMDCL = GetFormattedCellValue(row, 10);
                                            string SRMDCLDF = GetCellTextValue(row, 11);
                                            string SRMDDJQHF = GetCellTextValue(row, 12);
                                            string SRMJJD = GetCellTextValue(row, 13);
                                            string SRMYCCLPD = GetCellTextValue(row, 14);
                                            string SRMBZJFZQ = GetCellTextValue(row, 15);
                                            string SRMSJJFRQ = GetCellTextValue(row, 16);
                                            string SRMCGZQDF = GetCellTextValue(row, 17);
                                            string SRMZF = GetCellTextValue(row, 18);
                                            string SRMPJ = GetCellTextValue(row, 19);
                                            //判断单据存在
                                            int count = await _SjDb.Queryable<SRMGYSPJ>()
                                        .Where(a => a.SRMGYSBH.Contains(SRMGYSBH) && a.SRMTIME == time)
                                        .Select(a => a.SRMID)
                                        .CountAsync();
                                    try
                                    {
                                        _SjDb.Ado.BeginTran();
                                        if (count == 0)//新增数据
                                        {
                                            SRMGYSPJ model = new SRMGYSPJ()
                                            {
                                                SRMID = Guid.NewGuid().ToString("N"),
                                                SRMTIME = time,
                                                SRMLB = SRMLB,
                                                SRMGYSBH = SRMGYSBH,
                                                SRMGYSMC = SRMGYSMC,
                                                SRMJYZPC = SRMJYZPC,
                                                SRMHGPC = SRMHGPC,
                                                SRMHGL = SRMHGL,
                                                SRMHGLDF = SRMHGLDF,
                                                SRMZPC = SRMZPC,
                                                SRMJSRKPC = SRMJSRKPC,
                                                SRMDCL = SRMDCL,
                                                SRMDCLDF = SRMDCLDF,
                                                SRMDDJQHF = SRMDDJQHF,
                                                SRMJJD = SRMJJD,
                                                SRMYCCLPD = SRMYCCLPD,
                                                SRMBZJFZQ = SRMBZJFZQ,
                                                SRMSJJFRQ = SRMSJJFRQ,
                                                SRMCGZQDF = SRMCGZQDF,
                                                SRMZF = SRMZF,
                                                SRMPJ = SRMPJ,
                                                SRMCREATETIME = DateTime.Now,
                                                SRMISDELETE = 0,
                                            };
                                            data.Add(model);
                                        }
                                        else
                                        {
                                            //批量修改
                                            int count1 = await _SjDb.Updateable<SRMGYSPJ>()
                       .SetColumns(a => a.SRMTIME == time)
                       .SetColumns(a => a.SRMLB == SRMLB)
                       .SetColumns(a => a.SRMGYSBH == SRMGYSBH)
                       .SetColumns(a => a.SRMGYSMC == SRMGYSMC)
                       .SetColumns(a => a.SRMJYZPC == SRMJYZPC)
                       .SetColumns(a => a.SRMHGPC == SRMHGPC)
                       .SetColumns(a => a.SRMHGL == SRMHGL)
                       .SetColumns(a => a.SRMHGLDF == SRMHGLDF)
                       .SetColumns(a => a.SRMZPC == SRMZPC)
                       .SetColumns(a => a.SRMJSRKPC == SRMJSRKPC)
                       .SetColumns(a => a.SRMDCL == SRMDCL)
                       .SetColumns(a => a.SRMDCLDF == SRMDCLDF)
                       .SetColumns(a => a.SRMDDJQHF == SRMDDJQHF)
                       .SetColumns(a => a.SRMJJD == SRMJJD)
                       .SetColumns(a => a.SRMYCCLPD == SRMYCCLPD)
                       .SetColumns(a => a.SRMBZJFZQ == SRMBZJFZQ)
                       .SetColumns(a => a.SRMSJJFRQ == SRMSJJFRQ)
                       .SetColumns(a => a.SRMCGZQDF == SRMCGZQDF)
                       .SetColumns(a => a.SRMZF == SRMZF)
                       .SetColumns(a => a.SRMPJ == SRMPJ)
                       .SetColumns(a => a.SRMCREATETIME == DateTime.Now)
                       .Where(a => a.SRMGYSBH.Contains(SRMGYSBH) && a.SRMTIME == time)
                       .ExecuteCommandAsync();
                                        }
                                        _SjDb.Ado.CommitTran();
                                    }
                                    catch (Exception e)
                                    {
                                        _SjDb.Ado.RollbackTran();
                                        throw Oops.Oh($"异常：{e.Message}");
                                    }
                                }
                                }
                                }
                            }
                            // 与数据库交互
                            int cnt = await _SjDb.Insertable(data).ExecuteCommandAsync();
                            return "导入成功";
                        }
                        else
                        {
                            return "表中没有数据"; ;
                        }
                    }
                    else
                    {
                        return "导入文件格式有误";
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }

        }

        private string GetFormattedCellValue(IRow row, int column)
        {
            var cell = row.GetCell(column, MissingCellPolicy.RETURN_BLANK_AS_NULL);

            if (cell != null)
            {
                var dataFormatter = new DataFormatter();

                if (cell.CellType == CellType.Formula)
                {
                    var evaluator = cell.Sheet.Workbook.GetCreationHelper().CreateFormulaEvaluator();
                    var evaluatedCell = evaluator.EvaluateInCell(cell);
                    var formattedValue = dataFormatter.FormatCellValue(evaluatedCell);
                    return formattedValue;
                }
                else
                {
                    var formattedValue = dataFormatter.FormatCellValue(cell);
                    return formattedValue;
                }
            }

            return string.Empty;
        }

        private string GetCellTextValue(IRow row, int column)
            {
                var cell = row.GetCell(column, MissingCellPolicy.RETURN_BLANK_AS_NULL);

                if (cell != null)
                {
                    switch (cell.CellType)
                    {
                        case CellType.String:
                            return cell.StringCellValue;

                        case CellType.Numeric:
                            if (DateUtil.IsCellDateFormatted(cell))
                            {
                                return cell.DateCellValue.ToString();
                            }
                            else
                            {
                                return cell.NumericCellValue.ToString();
                            }

                        case CellType.Boolean:
                            return cell.BooleanCellValue.ToString();

                        case CellType.Formula:
                            switch (cell.CachedFormulaResultType)
                            {
                                case CellType.String:
                                    return cell.StringCellValue;

                                case CellType.Numeric:
                                    if (DateUtil.IsCellDateFormatted(cell))
                                    {
                                        return cell.DateCellValue.ToString();
                                    }
                                    else
                                    {
                                        return cell.NumericCellValue.ToDouble().ToString("0.00");
                                    }

                                case CellType.Boolean:
                                    return cell.BooleanCellValue.ToString();

                                default:
                                    return string.Empty;
                            }

                        default:
                            return string.Empty;
                    }
                }

                return string.Empty;
            }
    

        /// <summary>
        /// 某文件的md5值
        /// </summary>
        /// <param name="stream">file stream</param>
        /// <returns></returns>
        private string Md5FileHash(Stream stream)
        {
            using var md5 = MD5.Create();
            var data = md5.ComputeHash(stream);
            var sBuilder = new StringBuilder();
            for (var i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }

        #endregion

        /// <summary>
        ///  获取供应商评级列表-分页
        /// </summary>
        /// <returns></returns>
        [DisplayName("获取供应商评级列表-分页")]
        [HttpPost("/SRM/GetSupplierMarkPagedList")]
        public async Task<SqlSugarPagedList<SRMGYSPJ>> GetSupplierMarkPagedList(GetSrmSupplierMarkInput input)
        {
            if (_User.OrgId == "13901610784837")//供应商
            {
                var output = await _SjDb.Queryable<SRMGYSPJ>()
   .Where(l => l.SRMGYSBH == _User.UserId)
   .WhereIF(input.EndDate != null, (l) => (l.SRMTIME <= input.EndDate)) //结束日期
    .WhereIF(input.StartDate != null, (l) => (l.SRMTIME >= input.StartDate)) //开始日期
     .OrderBy(l => l.SRMTIME, OrderByType.Desc) //供应商评级年月排序
     .OrderBy(l => SqlFunc.ToDouble(l.SRMZF), OrderByType.Desc) //供应商评级总分
  .Select(l => new SRMGYSPJ
  {
      SRMID = l.SRMID,
      SRMLB = l.SRMLB,
      SRMTIME = l.SRMTIME,
      SRMGYSBH = l.SRMGYSBH,
      SRMGYSMC = l.SRMGYSMC,
      SRMJYZPC = l.SRMJYZPC,
      SRMHGPC = l.SRMJYZPC,
      SRMHGL = l.SRMHGL,
      SRMHGLDF = l.SRMHGLDF,
      SRMZPC = l.SRMZPC,
      SRMJSRKPC = l.SRMJSRKPC,
      SRMDCL = l.SRMDCL,
      SRMDCLDF = l.SRMDCLDF,
      SRMDDJQHF = l.SRMDDJQHF,
      SRMJJD = l.SRMJJD,
      SRMYCCLPD = l.SRMYCCLPD,
      SRMBZJFZQ = l.SRMBZJFZQ,
      SRMSJJFRQ = l.SRMSJJFRQ,
      SRMCGZQDF = l.SRMCGZQDF,
      SRMZF = l.SRMZF,
      SRMPJ = l.SRMPJ,
      SRMCREATETIME = l.SRMCREATETIME,
      SRMISDELETE = l.SRMISDELETE

  }).ToPagedListAsync(input.Page, input.PageSize);
                return output;
            }
            else
            {


                var output = await _SjDb.Queryable<SRMGYSPJ>()
           //.Where(c => c.Srmaa002 == _User.UserId)
           .WhereIF(input.EndDate != null, (l) => (l.SRMTIME <= input.EndDate)) //结束日期
            .WhereIF(input.StartDate != null, (l) => (l.SRMTIME >= input.StartDate)) //开始日期
          .WhereIF(!string.IsNullOrEmpty(input.SupplierName), (l) => (l.SRMGYSMC.Contains(input.SupplierName)) || (l.SRMGYSBH.Contains(input.SupplierName))) //供应商名称和编号

             .OrderBy(l => l.SRMTIME, OrderByType.Desc) //供应商评级年月排序
               .OrderBy(l => SqlFunc.ToDouble(l.SRMZF), OrderByType.Desc) //供应商评级总分
          .Select(l => new SRMGYSPJ
          {
              SRMID = l.SRMID,
              SRMLB = l.SRMLB,
              SRMTIME = l.SRMTIME,
              SRMGYSBH = l.SRMGYSBH,
              SRMGYSMC = l.SRMGYSMC,
              SRMJYZPC = l.SRMJYZPC,
              SRMHGPC = l.SRMJYZPC,
              SRMHGL = l.SRMHGL,
              SRMHGLDF = l.SRMHGLDF,
              SRMZPC = l.SRMZPC,
              SRMJSRKPC = l.SRMJSRKPC,
              SRMDCL = l.SRMDCL,
              SRMDCLDF = l.SRMDCLDF,
              SRMDDJQHF = l.SRMDDJQHF,
              SRMJJD = l.SRMJJD,
              SRMYCCLPD = l.SRMYCCLPD,
              SRMBZJFZQ = l.SRMBZJFZQ,
              SRMSJJFRQ = l.SRMSJJFRQ,
              SRMCGZQDF = l.SRMCGZQDF,
              SRMZF = l.SRMZF,
              SRMPJ = l.SRMPJ,
              SRMCREATETIME = l.SRMCREATETIME,
              SRMISDELETE = l.SRMISDELETE



          }).ToPagedListAsync(input.Page, input.PageSize);
                return output;
            }

        }


        /// <summary>
        ///  获取应商文件-分页
        /// </summary>
        /// <returns></returns>
        [HttpPost("/SRM/GetGYSFilePagedList")]
        public async Task<SqlSugarPagedList<SRMGYSFILE>> GetGYSFilePagedList(SrmGysFileInput input)
        {
            DateTime starttime = DateTime.MinValue; DateTime endtime = DateTime.MinValue;
            if (input.StartDate != null)
                starttime = (input.StartDate.ToDateTime().ToString("yyyy-MM-dd") + " 00:00:00").ToDateTime();
            if (input.EndDate != null)
                endtime = (input.EndDate.ToDateTime().ToString("yyyy-MM-dd") + " 23:59:59").ToDateTime();

            var output = await _SjDb.Queryable<SRMGYSFILE>()
.Where(l => l.ISDELETE == 0)
.WhereIF(input.EndDate != null, (l) => (l.CREATETIME <= endtime)) //结束日期
.WhereIF(input.StartDate != null, (l) => (l.CREATETIME >= input.StartDate)) //开始日期
.WhereIF(!string.IsNullOrEmpty(input.SRMGYS), (l) => (l.SSGYS.Contains(input.SRMGYS)))//供应商条件查询
.WhereIF(_User.OrgId == "13901610784837", (l) => (l.SSGYS.Contains(_User.UserId)))//供应商只能看到自己的数据
.OrderBy(l => l.CREATETIME, OrderByType.Desc) //模具建档日期排序
.Select(l => new SRMGYSFILE
{
    ID = l.ID,
    FILES = l.FILES,
    FTLETYPES = l.FTLETYPES,
    SSGYS = l.SSGYS,
    REMARK = l.REMARK,
    CREATETIME = l.CREATETIME

}).ToPagedListAsync(input.Page, input.PageSize);
            return output;
        }
        /// <summary>
        /// 增加供应商文件
        /// </summary>
        /// <returns></returns>
        [DisplayName("增加供应商文件")]
        [HttpPost("/SRM/AddGYSFile")]
        public async Task<string> AddGYSFile(SRMGYSFILE input)
        {
            //判断模具编号是否重复
            int count = await _SjDb.Queryable<SRMGYSFILE>()
                .Where(a => a.ID == input.ID)
                .Select(a => a.ID)
                .CountAsync();

            if (count > 0)
                throw Oops.Oh("数据已存在");
            var details = new SRMGYSFILE
            {
                ID = Guid.NewGuid().ToString("N"),
                FILES = input.FILES,
                FTLETYPES = input.FTLETYPES,
                SSGYS = input.SSGYS,
                REMARK = input.REMARK,
                CREATETIME = DateTime.Now,
                ISDELETE = input.ISDELETE
            };

            #region 插入事务
            try
            {
                //开启事务
                _SjDb.Ado.BeginTran();
                int cnt = await _SjDb.Insertable<SRMGYSFILE>(details).ExecuteCommandAsync();

                if (cnt == 0)
                {
                    throw Oops.Oh($"新建任务失败！");
                }
                //提交事务
                _SjDb.Ado.CommitTran();
                return "插入成功";
            }
            catch (Exception e)
            {
                //回滚事务
                _SjDb.Ado.RollbackTran();
                throw Oops.Oh($"异常：{e.Message}");
            }
            #endregion

        }
        /// <summary>
        /// 供应商文件更改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [DisplayName("供应商文件更改")]
        [HttpPost("/SRM/UpdateGYSFile")]
        public async Task<string> UpdateGYSFile(SRMGYSFILE input)
        {
            int cnt = await _SjDb.Queryable<SRMGYSFILE>()
                            .Where(a => a.ID == input.ID)
                             .Select(a => a.ID).CountAsync();

            if (cnt == 0)
            {
                throw Oops.Oh("任务不存在，驳回！");
            }
            else
            {
                //提交
                try
                {
                    _SjDb.Ado.BeginTran();
                    int count = await _SjDb.Updateable<SRMGYSFILE>()
                        .SetColumns(a => a.FTLETYPES == input.FTLETYPES)
                        .SetColumns(a => a.FILES == input.FILES)
                        .SetColumns(a => a.REMARK == input.REMARK)
                        .SetColumns(a => a.SSGYS == input.SSGYS)
                        .Where(a => a.ID == input.ID)

                        .ExecuteCommandAsync();

                    if (count != 1)
                    {
                        throw Oops.Oh("修改失败！");
                    }
                    _SjDb.Ado.CommitTran();
                    return "修改成功！";
                }
                catch (Exception e)
                {
                    _SjDb.Ado.RollbackTran();
                    throw Oops.Oh($"异常：{e.Message}");
                }
            }



        }
        /// <summary>
        /// 供应商法文件删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [DisplayName("供应商法文件删除")]
        [HttpPost("/SRM/DeleteGYSFile")]
        public async Task<string> DeleteGYSFile(DeleteGYSFile input)
        {
            //判断单据存在
            int cnt = await _SjDb.Queryable<SRMGYSFILE>()
                .Where(a => a.ID == input.ID)
                .Select(a => a.ID)
                .CountAsync();
            if (cnt == 0)
            {
                throw Oops.Oh("任务不存在，驳回！");
            }
            else
            {
                //提交
                try
                {
                    _SjDb.Ado.BeginTran();
                    int count = await _SjDb.Updateable<SRMGYSFILE>()
                        .SetColumns(a => a.ISDELETE == 1)
                        .Where(a => a.ID == input.ID)
                        .ExecuteCommandAsync();

                    if (count != 1)
                    {
                        throw Oops.Oh("删除失败！");
                    }
                    _SjDb.Ado.CommitTran();
                    return "删除成功！";
                }
                catch (Exception e)
                {
                    _SjDb.Ado.RollbackTran();
                    throw Oops.Oh($"异常：{e.Message}");
                }
            }
        }
    }
}

